<?php
/**
 *
 * CmsArtCatRepository.php
 * User: kalvin
 * Date: 2018/4/8
 * Time: 下午2:47
 */

namespace App\System\Activity\Repositories;


use App\Module\Api\Controllers\Request\TableRequest;
use App\Module\Api\Controllers\Request\TableWhereRequest;
use App\System\Activity\Model\ActivityInfo;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Oms\Model\OrderInfo;


class ActiveInfoRepository extends Repository
{
    use SinglePattern;
    public function getActiveTitleById($id,$field)
    {
        $data=ActivityInfo::select($field)->find($id);
        if (!$data){
            return false;
        }
        return $data->getAttributeValue($field);
    }
    public function getActiveInfoById($id)
    {
        return ActivityInfo::find($id);
    }

    /**
     * API 获取记录列表
     * @param TableRequest $request
     * @param TableWhereRequest $tableWhereRequest
     * @param $user_id
     * @return mixed
     */
    public function getApiList(TableRequest $request,TableWhereRequest $tableWhereRequest,$user_id)
    {
        $status = $tableWhereRequest->status;
//        查询有赠送记录的数据
        $data = OrderInfo::where("oms_order_info.user_id",$user_id)
            ->whereNull('oms_order_info.giving_id')
            ->where('oms_order_goods.type','=','2')
            ->select('oms_order_info.id','oms_order_info.trade_no','oms_order_info.status','oms_order_goods.name','oms_order_info.real_amount as realAmount','actvity_info.start_at as startAt','actvity_info.deadline_at as deadlineAt','oms_order_info.created_at as createdAt','actvity_info.type as actType')
            ->leftjoin('oms_order_goods','oms_order_goods.order_info_id','=','oms_order_info.id')
            ->leftjoin('actvity_info','oms_order_goods.foreign_key_id','=','actvity_info.id')
            ->when($status,function($query) use($status){
                return $query->where('oms_order_info.status',$status);
            })

            ->orderBy('oms_order_goods.'.$request->orderBy,$request->orderType)
            ->paginate($request->limit,['*'],'page',$request->page);


        $return['orderList'] = array();
        foreach($data as $v) {
            $item = [];
            $item['tradeId'] = $v->id;
            $item['tradeNo'] = $v->trade_no;
            $item['status'] = $v->status;
            $item['type'] = $v->actType;
            $item['name'] = $v->name;
            $item['realAmount'] = $v->realAmount;
            $item['startAt'] = $v->startAt;
            $item['deadlineAt'] = $v->deadlineAt;
            $item['createdAt'] = $v->createdAt;
            $return['orderList'][] = $item;
        }
        $return['total'] =$data->total();
        return $return;
    }
}